package com.SouthernWall_404.AgeSuitcase.Common.API.Info;

import com.SouthernWall_404.AgeSuitcase.Common.AgeSuitcase;
import com.SouthernWall_404.AgeSuitcase.Common.world.item.ConstractItem;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;

import java.util.ArrayList;
import java.util.List;

public class ConstractInfo {

    public static String A_TAG="a";
    public static String B_TAG="b";
    public static String CONSTENT_TAG="constent";
    public static String LISTPACK_SIZE_TAG="size";

    public List<String> A;//甲方
    public List<String> B;//乙方

    public AbstractContent content;//合约内容
    public ConstractInfo(List<String> A, List<String> B, AbstractContent content)
    {
        this.A=A;
        this.B=B;
        this.content=content;
    }
    public ConstractInfo(CompoundTag constractPack)
    {
        this.loadConstractPack(constractPack);
    }
    public ConstractInfo(ItemStack itemStack)
    {
        CompoundTag nbt=itemStack.getOrCreateTag();
        ConstractInfo constractInfo=new ConstractInfo(List.of(""),List.of(""),null);
        if(nbt.contains(AgeSuitcase.MODID))
        {
            CompoundTag modPack=nbt.getCompound(AgeSuitcase.MODID);
            if(modPack.contains(ConstractItem.CONSTRACT_TAG))
            {
                CompoundTag constractPack=modPack.getCompound(ConstractItem.CONSTRACT_TAG);
                this.loadConstractPack(constractPack);

            }
        }
    }
    public CompoundTag getAPack()
    {
        return getListPack(this.A);
    }
    public CompoundTag getBPack()
    {
        return getListPack(this.B);
    }

    public CompoundTag getListPack(List<String> names)
    {
        CompoundTag namePack=new CompoundTag();

        namePack.putInt(LISTPACK_SIZE_TAG,names.size());
        for(int i=0;i<names.size();i++)
        {
            String name=names.get(i);
            namePack.putString(""+i,name);
        }

        return namePack;
    }


    public CompoundTag getConstractPack()//保存时获取整个constract包
    {
        CompoundTag constractPack=new CompoundTag();

        CompoundTag APack=this.getAPack();
        CompoundTag BPack=this.getBPack();
        CompoundTag contentPack=this.content.getOrCreateContentPack();

        constractPack.put(A_TAG,APack);
        constractPack.put(B_TAG,BPack);
        if(contentPack!=null)
        {
            constractPack.put(CONSTENT_TAG,contentPack);
        }
        return constractPack;
    }

    //准备写读取包
    public List<String> loadListPack(CompoundTag namePack)
    {
        List<String> names=new ArrayList<>();
        int size=0;

        if(namePack.contains(LISTPACK_SIZE_TAG))
        {
            size=namePack.getInt(LISTPACK_SIZE_TAG);
        }

        for(int i=0;i<size;i++)
        {
            if(namePack.contains(""+i))
            {
                names.add(namePack.getString(""+i));
            }
        }

        return names;
    }

    public void loadConstractPack(CompoundTag constractPack)
    {
        if(constractPack.contains(A_TAG))
        {
            this.A=loadListPack(constractPack.getCompound(A_TAG));
        }
        if(constractPack.contains(B_TAG))
        {
            this.B=loadListPack(constractPack.getCompound(B_TAG));
        }
        if(constractPack.contains(CONSTENT_TAG))
        {
//            AbstractContent content1=new
//            this.content=
        }
    }

}
